package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    CategoryMapper categoryMapper;

    @Override
    public PageResult findAll(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        List<Category> categoryList = categoryMapper.selectAll(categoryPageQueryDTO.getName(), categoryPageQueryDTO.getType());
        Page<Category> page = (Page<Category>) categoryList;
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Integer modify(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        //以下两个字段属于公共字段,已经通过AOP技术实现自动填充
        /*category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());*/
        return categoryMapper.update(category);
    }

    @Override
    public Integer setStatus(Long id, Integer status) {
        Category category = Category.builder()
                .id(id)
                .status(status)
                //以下两个字段属于公共字段,已经通过AOP技术实现自动填充
                /*.updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())*/
                .build();
        return categoryMapper.update(category);

    }

    @Override
    public Integer add(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(StatusConstant.ENABLE);
        //以下四个字段属于公共字段,已经通过AOP技术实现自动填充
        /*category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());*/
        return categoryMapper.insert(category);
    }

    @Override
    public Integer remove(Long id) {
        return categoryMapper.delete(id);
    }

    @Override
    public List<Category> findByType(Integer type) {
        return categoryMapper.selectByType(type);
    }
}